<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="类型名字" prop="label">
        <el-input v-model="dataForm.label" placeholder="类型名字"></el-input>
      </el-form-item>
      <el-form-item label="阈值(小时)" prop="value">
        <el-input-number v-model="dataForm.threshold" placeholder="阈值" :precision="1" :step="0.1"
                         :max="10"></el-input-number>
      </el-form-item>
      <el-form-item label="类型值" prop="value">
        <el-input v-model="dataForm.value" placeholder="类型值"></el-input>
      </el-form-item>
      <el-form-item label="线名称" prop="value">
        <el-tag
          :key="tag"
          v-for="tag in dataForm.lineNameData"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)">
          {{ tag }}
        </el-tag>
        <el-input
          class="input-new-tag"
          v-if="inputVisible"
          v-model="inputValue"
          ref="saveTagInput"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        >
        </el-input>
        <el-button v-if="buttonVisible" v-else class="button-new-tag" size="small" @click="showInput">+ New Tag
        </el-button>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        detailId: 0,
        dictId: '',
        label: '',
        value: '',
        dictSort: 0,
        createBy: '',
        updateBy: '',
        createTime: '',
        updateTime: '',
        threshold: 0.5,
        lineCount: 2,
        lineNameData: []
      },
      dataRule: {
        label: [
          {required: true, message: '字典标签不能为空', trigger: 'blur'}
        ],
        threshold: [
          {required: true, message: '字典值不能为空', trigger: 'blur'}
        ]
        // dictSort: [
        //   {required: true, message: '排序不能为空', trigger: 'blur'}
        // ]
      },
      dynamicTags: ['标签一', '标签二', '标签三'],
      inputVisible: false,
      buttonVisible: true,
      inputValue: ''
    }
  },
  methods: {
    init (id) {
      this.dataForm.detailId = id || 0
      if (this.dataForm.detailId === 0) {
        this.dataForm.lineCount = 2
      }
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.detailId) {
          this.$http({
            url: this.$http.adornUrl(`/sys/sysdictdetail/info/${this.dataForm.detailId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.dictId = data.sysDictDetail.dictId
              this.dataForm.label = data.sysDictDetail.label
              this.dataForm.threshold = data.sysDictDetail.threshold
              this.dataForm.value = data.sysDictDetail.value
              this.dataForm.lineCount = data.sysDictDetail.lineCount
              this.dataForm.lineNameData = data.sysDictDetail.lineNameData
              if (this.dataForm.lineNameData.length >= this.dataForm.lineCount) {
                this.buttonVisible = false
              }
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      if (this.dataForm.lineNameData.length < this.dataForm.lineCount) {
        this.$message.error('该类型设备有' + this.dataForm.lineCount + '条线，请添加折线名称。')
        return
      }
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sys/sysdictdetail/${!this.dataForm.detailId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'detailId': this.dataForm.detailId || undefined,
              'dictId': 1,
              'label': this.dataForm.label,
              'value': this.dataForm.value,
              'threshold': this.dataForm.threshold,
              'lineCount': this.dataForm.lineCount,
              'lineNameData': this.dataForm.lineNameData
              // 'dictSort': this.dataForm.dictSort
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    handleClose (tag) {
      this.dataForm.lineNameData.splice(this.dataForm.lineNameData.indexOf(tag), 1)
      console.info(this.dataForm.lineNameData.length)
      if (this.dataForm.lineNameData.length < this.dataForm.lineCount) {
        this.buttonVisible = true
      }
    },

    showInput () {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    handleInputConfirm () {
      let inputValue = this.inputValue
      if (inputValue) {
        this.dataForm.lineNameData.push(inputValue)
      }
      if (this.dataForm.lineNameData.length >= this.dataForm.lineCount) {
        this.buttonVisible = false
      } else {
        this.buttonVisible = true
      }
      this.inputVisible = false
      this.inputValue = ''
    }
  }
}
</script>
<style scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}

.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}

.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
